<template>
  <div id="detail">
    <detail-nav-bar ref="navbar" class="detail-nav" @titleClick="titleClick" />
    <scroll class="content" ref="scroll" @scroll="contentScroll" :probe-type="3">
    <detail-swiper :topImages="topImages"/>
    <detail-base-info :goods="goods"/>
    <detail-shop-info :shop="shop"/>
    <detail-goods-info :detailInfo="detailInfo" @image-load="imageLoad"/>
    <detail-param-info ref="params" :paramInfo="paramInfo"/>
    <detail-commont-info ref="comment" :commentInfo="commentInfo"/>
    <goods-list ref="recommend" :goods="recommends"/>
    </scroll>
    <back-top @click.native="backClick" v-show="IsBackTop"/>
    <detail-bottom-bar @add-to-cart="addToCart"/>
    <toast ref="toast"/>
  </div>
</template>

<script>
import DetailNavBar from './childComps/DetailNavBar'
import {getDetail, Goods, Shop, GoodsParam, getRecommend} from 'api/detail'
import DetailSwiper from './childComps/DetailSwiper'
import DetailBaseInfo from './childComps/DetailBaseInfo'
import DetailShopInfo from './childComps/DetailShopInfo.vue'
import Scroll from 'components/common/scroll/Scroll.vue'
import DetailGoodsInfo from './childComps/DetailGoodsInfo.vue'
import DetailParamInfo from './childComps/DetailParamInfo.vue'
import DetailCommontInfo from './childComps/DetailCommontInfo.vue'
import GoodsList from '../../components/content/goods/GoodsList.vue'
import {itemListenerMixin, backTopMixin} from 'common/mixin'
import DetailBottomBar from './childComps/DetailBottomBar.vue';
import Toast from 'components/common/toast/Toast.vue'


export default {
  components: {
    DetailNavBar,
    DetailSwiper,
    DetailBaseInfo,
    DetailShopInfo,
    Scroll,
    DetailGoodsInfo,
    DetailParamInfo,
    DetailCommontInfo,
    GoodsList,
    DetailBottomBar,
    Toast,
  },
  name: 'Detail',
  data(){
    return {
      iid: null,
      topImages: [],
      goods: {},
      shop: {},
      detailInfo: {},
      paramInfo: {},
      commentInfo: {},
      recommends: [],
      itemImgListener: null,
      themeTopYs: []
    }
  },
  methods: {
    imageLoad(){
      // console.log('load');
      this.$refs.scroll.refresh()
      this.themeTopYs = []
      this.themeTopYs.push(0)
      this.themeTopYs.push(this.$refs.params.$el.offsetTop-44)
      this.themeTopYs.push(this.$refs.comment.$el.offsetTop-44)
      this.themeTopYs.push(this.$refs.recommend.$el.offsetTop-44)
      // console.log(this.themeTopYs);
    },
    titleClick(currentIndex){
      // console.log(currentIndex);
      this.$refs.scroll.scrollTo(0,-this.themeTopYs[currentIndex],100)
    },
    contentScroll(position){
      // console.log(position.y);
      // console.log(this.themeTopYs);
      // if(-position.y >= this.themeTopYs[0] && -position.y < this.themeTopYs[1]){
      //   this.$refs.navbar.currentIndex = 0
      // }else if(-position.y >= this.themeTopYs[1] && -position.y <= this.themeTopYs[2]){
      //   this.$refs.navbar.currentIndex = 1
      // }else if(-position.y >= this.themeTopYs[2] && -position.y <= this.themeTopYs[3]){
      //   this.$refs.navbar.currentIndex = 2
      // }else if(-position.y > this.themeTopYs[3]){
      //   this.$refs.navbar.currentIndex = 3
      // }

      const length = this.themeTopYs.length
      for(let i = 0;i<this.themeTopYs.length;i++){
        if(this.$refs.navbar.currentIndex !== i){
          if((i<length-1 && -position.y>=this.themeTopYs[i] && -position.y<this.themeTopYs[i+1]) ||
           (i === length-1 && -position.y >= this.themeTopYs[i])){
            this.$refs.navbar.currentIndex = i;
            // console.log(i);
          }
        }
      }

      this.listenShowBackTop(position)
    },
    addToCart(){
      const product = {}
      product.image = this.topImages[0]
      product.title = this.goods.title
      product.desc = this.goods.desc
      product.price = this.goods.realPrice
      product.iid = this.iid
      this.$store.dispatch('addCart', product).then(res=>{
        console.log(this.$toast);
        this.$toast.show(res)

      })
    }
  },
  created(){
    this.iid = this.$route.params.iid
    getDetail(this.iid).then(res => {
      const data = res.result

      // 1.获取顶部的图片轮播数据,
      this.topImages = data.itemInfo.topImages;

      // 2.获取商品信息
      this.goods = new Goods(data.itemInfo, data.columns, data.shopInfo.services);
      // console.log(this.goods);

      // 3.创建店铺信息对象
      this.shop = new Shop(data.shopInfo)

      // 4.保存商品的详情数据
      this.detailInfo = data.detailInfo;

      // 5.获取参数信息
      this.paramInfo = new GoodsParam(data.itemParams.info, data.itemParams.rule)

      // 6.取出评论信息
      if(data.rate.cRate !== 0){
        this.commentInfo = data.rate.list[0]
      }
    })
    getRecommend().then(res => {
      this.recommends = res.data.list
    })



  },
  mounted(){

  },
  updated(){

  },
  destroyed(){
    this.$bus.$off('itemImageLoad',this.itemImgListener)
  },
  mixins: [itemListenerMixin, backTopMixin],
}
</script>

<style scoped>
  #detail {
    position: relative;
    z-index: 9;
    background-color: #fff;
    height: 100vh;
  }

  .detail-nav {
    position: relative;
    z-index: 9;
    background-color: #fff;
  }

  .content {
    height: calc(100% - 44px - 49px);
  }
</style>
